package com.app.manage.moudles.push.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import com.app.manage.moudles.operate.service.OperateService;
import com.app.manage.moudles.push.constants.TaskStepEnums;
import com.app.manage.moudles.push.entity.TPushTask;
import com.app.manage.moudles.push.pojo.TaskSearchPojo;
import com.app.manage.moudles.push.service.TPushTaskService;
import com.app.manage.moudles.server.entity.TServer;
import com.app.manage.utils.PageSearch;
import com.app.manage.utils.Rsp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("task")
@Api(tags = "发布任务")
public class TaskController {

    @Resource
    private TPushTaskService tPushTaskService;

    @Resource
    private OperateService operateService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    @PostMapping("pageList")
    @ApiOperation("分页查询任务信息")
    public Rsp page(@RequestBody PageSearch<TaskSearchPojo> s){
        return Rsp.success(tPushTaskService.pageData(s.getPageNum(),s.getPageSize(),s.getDomain()));
    }

    @GetMapping("getById")
    @ApiOperation("根据主键查询")
    public Rsp getById(Integer id){
        if(id  != null){
           return Rsp.success( tPushTaskService.findById(id));
        }else {
            return Rsp.success(tPushTaskService.buildNewTask());
        }
    }

    @PostMapping("saveTask")
    @ApiOperation("保存")
    public Rsp saveTask(@RequestBody TPushTask tPushTask){
        tPushTaskService.saveTask(tPushTask);
        return Rsp.success();
    }


    @GetMapping("listByDate")
    @ApiOperation("根据一天查询")
    public Rsp   listByDate(String dateStr,Integer taskId){
        //2025-04-00
        String startTime = dateStr.substring(0,10)+" 00:00:00";
        String endTime = dateStr.substring(0,10)+" 23:59:59";
        LambdaQueryWrapper<TPushTask> query = new LambdaQueryWrapper<>();
        query.ge(TPushTask::getPushTime,startTime);
        query.le(TPushTask::getPushTime,dateStr);
        query.eq(taskId != null,TPushTask::getId,taskId);
        query.in(TPushTask::getPushStep,TaskStepEnums.findActSteps());
        List<TPushTask> taskList  = tPushTaskService.list(query);
        taskList.forEach(o->{
            o.setPushStepName(TaskStepEnums.getNameByStep(o.getPushStep()));
        });
        return Rsp.success(taskList);
    }

    @GetMapping("removeOldTask")
    @ApiOperation("删除任务")
    public Rsp removeOldTask(String ids){
        if(StringUtils.isEmpty(ids)){
            return Rsp.fail("请选择一个数据删除");
        }
        for(String id : ids.split(",")){
            tPushTaskService.removeOld(Integer.parseInt(id));
        }
        return Rsp.success("删除成功！");
    }

    @GetMapping("stopTask")
    @ApiOperation("停止任务")
    public Rsp stopTask(Integer id){
        String res =  tPushTaskService.stopTask(id);
        if(StringUtils.isNotEmpty(res)){
            return Rsp.fail(res);
        }
        return Rsp.success();
    }

    @GetMapping("restartTask")
    @ApiOperation("重启任务")
    public Rsp restartTask(Integer id){
        String res =  tPushTaskService.restartTask(id);
        if(StringUtils.isNotEmpty(res)){
            return Rsp.fail(res);
        }
        return Rsp.success();
    }


}
